﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Programonica.Igramonica.UserInterface
{
    public class LabelControl : GameControl
    {
        #region Поля

        String m_text;

        Texture2D m_labelTexture;

        #endregion

        //---------------------------------------------------------------------------

        #region Свойства

        public String Text
        {
            set
            {
                m_text = value;
            }
            get
            {
                return m_text;
            }
        }

        #endregion

        //---------------------------------------------------------------------------
        
        #region Инициализация

        public LabelControl()
        {

        }

        //---------------------------------------------------------------------------

        public override void LoadContent() 
        {
            m_labelTexture = Content.Load<Texture2D>("Label");

            base.LoadContent();
        }

        //---------------------------------------------------------------------------

        public override void UnloadContent() 
        {
            base.UnloadContent();
        }

        #endregion

        //---------------------------------------------------------------------------

        #region Обновление и отображение

        public override void HandleInput()
        {
        }

        //---------------------------------------------------------------------------

        public override void Draw()
        {
            ScreenManager screenManager = ParentScreen.ScreenManager;

            GraphicsDevice graphicsDevice = ParentScreen.ScreenManager.GraphicsDevice;

            float textWidth = screenManager.LabelFont.TextWidth(Text, 0.5f);

            float textHeight = screenManager.LabelFont.TextHeight(Text, 0.5f);

            Vector2 labelCenter = new Vector2(m_labelTexture.Width / 2.0f, m_labelTexture.Height / 2.0f);

            Vector2 textPosition = Position + new Vector2(labelCenter.X - textWidth / 2.0f, labelCenter.Y - textHeight / 2.0f);

            screenManager.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

            screenManager.SpriteBatch.Draw(m_labelTexture, Position + ParentScreen.PositionWithOffset, Color.White);

            screenManager.LabelFont.Draw(Text, textPosition + ParentScreen.PositionWithOffset, 0.5f, Color.White, screenManager.SpriteBatch);

            screenManager.SpriteBatch.End();
        }

        #endregion
    }
}
